package com.ruoyi.web.controller.mine;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mine.domain.MyConcern;
import com.ruoyi.mine.service.IMyConcernService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的关注Controller
 *
 * @author ruoyi
 * @date 2024-07-26
 */
@Anonymous
@RestController
@RequestMapping("/mine/concern")
public class MyConcernController extends BaseController
{
    @Autowired
    private IMyConcernService myConcernService;

    /**
     * 查询我的关注列表
     */
//    @PreAuthorize("@ss.hasPermi('mine:concern:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyConcern myConcern)
    {
        startPage();
        List<MyConcern> list = myConcernService.selectMyConcernList(myConcern);
        return getDataTable(list);
    }

    /**
     * 导出我的关注列表
     */
//    @PreAuthorize("@ss.hasPermi('mine:concern:export')")
    @Log(title = "我的关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MyConcern myConcern)
    {
        List<MyConcern> list = myConcernService.selectMyConcernList(myConcern);
        ExcelUtil<MyConcern> util = new ExcelUtil<MyConcern>(MyConcern.class);
        util.exportExcel(response, list, "我的关注数据");
    }

    /**
     * 获取我的关注详细信息
     */
//    @PreAuthorize("@ss.hasPermi('mine:concern:query')")
    @GetMapping(value = "/{concernId}")
    public AjaxResult getInfo(@PathVariable("concernId") Long concernId)
    {
        return success(myConcernService.selectMyConcernByConcernId(concernId));
    }

    /**
     * 新增我的关注
     */
//    @PreAuthorize("@ss.hasPermi('mine:concern:add')")
    @Log(title = "我的关注", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyConcern myConcern)
    {
        Long concernUserId = myConcern.getConcernUserId();
        Long concernBeUserId = myConcern.getConcernBeUserId();

        boolean unique = myConcernService.checkUserUnique(concernUserId, concernBeUserId);
        if(!unique)
            return toAjax(myConcernService.insertMyConcern(myConcern));

        else{
            return error("已关注");
        }

    }

    /**
     * 修改我的关注
     */
//    @PreAuthorize("@ss.hasPermi('mine:concern:edit')")
    @Log(title = "我的关注", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyConcern myConcern)
    {
        return toAjax(myConcernService.updateMyConcern(myConcern));
    }

    /**
     * 删除我的关注
     */
//    @PreAuthorize("@ss.hasPermi('mine:concern:remove')")
    @Log(title = "我的关注", businessType = BusinessType.DELETE)
	@DeleteMapping("/{concernIds}")
    public AjaxResult remove(@PathVariable Long[] concernIds)
    {
        return toAjax(myConcernService.deleteMyConcernByConcernIds(concernIds));
    }
}
